# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: pandada8 <pandada8@gmail.com>

pkgname=v2ray
pkgver=5.4.1
_commit=9b5262855fd5bf432c5abdd24152a9089bbb874f
pkgrel=1
pkgdesc="A platform for building proxies to bypass network restrictions"
arch=('loong64' 'x86_64')
url="https://github.com/v2fly/v2ray-core"
license=('MIT')
depends=('glibc' 'v2ray-domain-list-community' 'v2ray-geoip')
makedepends=('go' 'git')
backup=(etc/v2ray/config.json)
source=("git+https://github.com/v2fly/v2ray-core.git#commit=$_commit")
sha512sums=('SKIP')

prepare() {
  cd v2ray-core
  sed -i 's|/usr/local/bin|/usr/bin|;s|/usr/local/etc|/etc|' release/config/systemd/system/*.service
}

build() {
  cd v2ray-core
  export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external"
  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export GOPROXY=https://goproxy.cn
  go mod edit -replace=golang.org/x/sys=github.com/golang/sys@v0.0.0-20220622161953-175b2fd9d664
  go mod edit -replace=golang.org/x/net=github.com/golang/net@v0.0.0-20220622184535-263ec571b305
  go mod tidy
  go build -o v2ray ./main
}

check() {
  cd v2ray-core
  go test -p 1 -tags json -v -timeout 30m ./...
}

package() {
  cd v2ray-core
  install -Dm644 LICENSE -t "$pkgdir"/usr/share/licenses/v2ray/
  install -Dm644 release/config/systemd/system/v2ray.service -t "$pkgdir"/usr/lib/systemd/system/
  install -Dm644 release/config/systemd/system/v2ray@.service -t "$pkgdir"/usr/lib/systemd/system/
  install -Dm644 release/config/*.json -t "$pkgdir"/etc/v2ray/
  install -Dm755 v2ray -t "$pkgdir"/usr/bin/
}
